package com.oocl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.entity.SessionInfo;
import com.oocl.service.FirstPageService;

@Controller
public class FirstPageController {
	
	@Resource(name="firstPageService")
	private FirstPageService firstPageService;
	
	
	
	@RequestMapping(value="/showMerchants",method=RequestMethod.GET)
	public String showMerchants(Model model){
		List<Merchants> merchants = firstPageService.showMerchants();
		
		model.addAttribute("merchants",merchants);
		return "welcome";
	}
	
	
	@RequestMapping(value="/toCartPage")
	public String toCartPage(String icnum,Model model) throws Exception {
		List<Foods> foods = firstPageService.showFoodsByMerchant(icnum);
		Merchants merchant = firstPageService.findmerchantsByIcnum(icnum);
		String levelString = firstPageService.computeRatingsLevelWithIcnum(icnum);
		model.addAttribute("levelString",levelString);
		model.addAttribute("foods",foods);
		model.addAttribute("merchant",merchant);
		return "cart";
	}
	
	@ResponseBody
	@RequestMapping(value="/findMerchantsByInputText")
	public List<Merchants> findMerchantsByInputText(String text){
		List<Merchants> merchantList = null;
		if("".equals(text)){
			merchantList = firstPageService.showMerchants();
			return merchantList;
		}
		merchantList = firstPageService.findmerchantsByMname(text);
		return merchantList;
	}
	
	
	
}
